<?php
$parentCaption = BUtils::T('gallery_lbl');
$this->pageTitle = $parentCaption;

$this->breadcrumbs = array(
	$parentCaption,
);

$upload_photo_btn = BUtils::T('upload_photo_btn');
?>
<div class="page-header position-relative">
	<h1>
		<?php echo $parentCaption; ?>
	</h1>
</div><!--/.page-header-->

<div class="row-fluid">
	<!-- Message -->
	<div class="alert alert-block alert-success">
		<button type="button" class="close" data-dismiss="alert">
			<i class="icon-remove"></i>
		</button>
		<i class="icon-info-sign green"></i>
		<?php echo BUtils::T('gallery_tips_eg'); ?>
	</div>
	
	<div class="space-6"></div>
	
	<div class="row-fluid">
		<div class="span12">
			<div class="widget-box transparent">
				<div style="height:80px;" class="clearfix">
<?php $this->widget('ext.EAjaxUpload.EAjaxUpload', array(
	'id' => 'uploadFile',
	'postParams' => array('prefix' => 'gal_'),
	'config' => array(
		'buttonName' => $upload_photo_btn,
		'action' => Yii::app()->createUrl('/gallery/uploadGallery'),
		'allowedExtensions'=>array("jpg","jpeg","gif","png"),
		'sizeLimit'=>8*1024*1024,
		'onComplete' => "js:function(id, fileName, responseJSON){ handleUploaded(responseJSON); }",
	),
)); ?>	
				</div>
				<div class="widget-body">
					<div class="widget-body-inner">
					<div class="widget-main">
<?php
if (count($gallery_rows) > 0) {
	echo '<div class="gallery">';
	foreach ($gallery_rows as $r) {
		$url_thumb = BUtils::GetOfferThumbUrl($r['url_picture']);
		$url_picture = BUtils::GetOfferPictureUrl(str_replace('thumb_', '', $r['url_picture']));
		$comments = Yii::app()->locale->dateFormatter->formatDateTime($r['ts_create_time']);
		$image_close = Yii::app()->baseUrl . '/img/btn-close.png';
		echo <<<HTML
<div class="photo relative" id="{$r['id']}">
	<div class="actions">
		<a href="#dlg-delete" role="button" class="remove-icon" data-toggle="modal" data-id="{$r['id']}" data-url="{$r['url_picture']}"><img src="{$image_close}" /></a>
	</div>
	<div class="thumb-wrap">
		<a href="{$url_picture}" data-rel="colorbox"><img src="{$url_thumb}" alt="" /></a>
	</div>
	<div class="comments">
		<span>{$comments}</span>
	</div>
	<div class="clearfix"></div>
</div>
HTML;
	}
	echo '</div>';
	echo '<div class="clear"></div>';
}
?>
					</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
</div><!--/row-->

<!-- Modal -->
<div id="dlg-delete" class="modal hide" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
		<h3><?php echo BUtils::T('confirm_photo_delete_lbl'); ?></h3>
	</div>
	<div class="modal-body">
		<p><?php echo BUtils::T('confirm_photo_delete_msg'); ?></p>
	</div>
	<div class="modal-footer">
		<button class="btn" data-dismiss="modal" aria-hidden="true"><?php echo BUtils::T('cancel_btn'); ?></button>
		<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true" id="dlg-delete-confirm"><?php echo BUtils::T('confirm_btn'); ?></button>
	</div>
</div>

<script type="text/javascript">
$(function() {
	var colorbox_params = {
		reposition:true,
		scalePhotos:true,
		scrolling:false,
		previous:'<i class="icon-arrow-left"></i>',
		next:'<i class="icon-arrow-right"></i>',
		current:'{current} of {total}',
		maxWidth:'100%',
		maxHeight:'100%',
		onOpen:function(){
			document.body.style.overflow = 'hidden';
		},
		onClosed:function(){
			document.body.style.overflow = 'auto';
		},
		onComplete:function(){
			$.colorbox.resize();
		}
	};
	$('.photo [data-rel="colorbox"]').colorbox(colorbox_params);
	
	//show delete button
	$('.photo').mouseenter(function() {
		$(this).addClass('selected').children('.actions').show();
	}).mouseleave(function(){
		$(this).removeClass('selected').children('.actions').hide();
    });
	
	//delete button action
	var delete_id = null;
	var delete_url = null;
	$('.remove-icon').click(function(){
		delete_id = $(this).data('id');
		delete_url = $(this).data('url');
	});
	$('#dlg-delete-confirm').click(function() {
		if (delete_id) {
			$('#'+delete_id).remove();
			$.get('<?php echo Yii::app()->createUrl('/gallery/deleteGallery'); ?>', {id:delete_id, url:delete_url});
		}
	});
});
function handleUploaded(respJSON) {
	if (respJSON.error) {
		$('.qq-upload-button').html('<span>'+respJSON.error+'</span>');
	} else {
		//console.log(respJSON);
		window.location.reload();
	}
}
</script>